package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.videogo.exception.BaseException;
import com.videogo.openapi.bean.EZDeviceInfo;

import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.ui.vdieogo.util.EZUtils;
import cn.acewill.yxd.yunxundian.ui.vdieogo.util.VerifyCodeInput;

/**
 * Created by XXX on 2017/6/19.
 */

public class EZPlayerListAdapter extends BaseAdapter{

    private List<EZDeviceInfo> list;
    private Context context;
    private ViewOnClick onClick;
    public EZPlayerListAdapter(Context context,List<EZDeviceInfo> list,ViewOnClick onClick){
        this.context=context;
        this.list=list;
        this.onClick=onClick;
    }
    public List<EZDeviceInfo> getDeviceInfoList() {
        return list;
    }
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.ezplayinfo,parent,false);
            holder=new ViewHolder();
            holder.deviceName= (TextView) convertView.findViewById(R.id.deviceName);
            holder.player_ui= (ImageView) convertView.findViewById(R.id.player_ui);
            holder.deviceFrom= (TextView) convertView.findViewById(R.id.deviceFrom);
            holder.deviceStatus= (TextView) convertView.findViewById(R.id.deviceStatus);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        EZDeviceInfo info=list.get(position);
        holder.deviceName.setText(info.getDeviceName());
        holder.deviceFrom.setText(info.getDeviceType());

        if (info.getStatus()==1){
            holder.deviceStatus.setText("在线");
        }else{
            holder.deviceStatus.setText("不在线");
        }

        setImag(info,holder.player_ui);
        holder.player_ui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.toPlayVideo(position);
            }
        });

        return convertView;
    }

    class ViewHolder{
        TextView deviceName;
        ImageView player_ui;
        TextView deviceFrom;
        TextView deviceStatus;
    }

    public interface ViewOnClick{
        void toPlayVideo(int position);
    }

    public void setImag(EZDeviceInfo info,ImageView img){


      //    String url= info.getCameraInfoList().get(0).getCameraCover();
        if (info.getCameraInfoList().size()>0){
            int  num = info.getCameraInfoList().get(0).getCameraNo();
            try {
             String url =  YXDAPP.getOpenSDK().captureCamera(info.getDeviceSerial(),num);
                EZUtils.loadImage(context, img, url, info.getDeviceSerial(), new VerifyCodeInput.VerifyCodeErrorListener() {
                    @Override
                    public void verifyCodeError() {
                        Toast.makeText(context,"输入错误",Toast.LENGTH_LONG).show();
                    }

                });
            } catch (BaseException e) {
                e.printStackTrace();
            }
        }
    }

}
